package com.fancy.learncenter.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancy.learncenter.adapter.ReleaseWDPIcAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.DimenUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.view.CustomProgressDialog;
import com.fancy.learncenter.view.LPopWindow;
import com.fancy.learncenter.view.PopupWindowCity;
import com.fancy.learncenter.view.PopupWindowCutPic;
import com.fancy.learncenter.view.PopupWindowEducationType;
import com.fancy.learncenter.view.PopupWindowGoodsType;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReleaseXianZhiActivity extends TakePhotoActivity {

    @Bind(R.id.et_title)
    EditText etTitle;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.yibaocun)
    TextView yibaocun;
    @Bind(R.id.type_name)
    TextView typeName;
    @Bind(R.id.price_num)
    TextView priceNum;
    @Bind(R.id.next)
    TextView next;
    @Bind(R.id.choose_type)
    RelativeLayout chooseType;
    @Bind(R.id.price)
    RelativeLayout price;
    @Bind(R.id.submit)
    TextView submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_xianzhi);
        ButterKnife.bind(this);
        title.setText("发布宝贝");
        takePhoto = getTakePhoto();
        takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
        next.setVisibility(View.GONE);
        initRecycleView();

        popupWindowGoodsType = new PopupWindowGoodsType(this, recycleView);
        popupWindowGoodsType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
            @Override
            public void cancleCallBack() {

            }

            @Override
            public void selecCallBack(String content, String id) {
                typeName.setText(content);
                goodType = id;
            }

        });
    }

    Dialog dialog;

    String goodType = "";

    @OnClick({R.id.back, R.id.next, R.id.choose_type, R.id.price, R.id.yibaocun, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
//                LiZhiDialog.show(this, R.mipmap.bg_tuxing, "退出前，您需要保存吗？",
//                        true, "不保存", "保存", new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                finish();
//                                LiZhiDialog.dismissDialog();
//                            }
//                        }, new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                finish();
//                                LiZhiDialog.dismissDialog();
//                            }
//                        });

                finish();
                break;
            case R.id.choose_type:

                popupWindowGoodsType.showPopupWindow();
                break;
            case R.id.price:
                if (lPopWindow == null) {
                    showPicPopWindow(view);
                    lPopWindow.setDisCallBack(new LPopWindow.DisCallBack() {
                        @Override
                        public void disCallBack() {
                            if (!TextUtils.isEmpty(prince.getText().toString())) {
                                priceNum.setText("售价" + prince.getText().toString() + "元");
                            }

                        }
                    });
                }
                lPopWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);

                break;
            case R.id.yibaocun:

                if (popupWindowCity == null) {
                    popupWindowCity = new PopupWindowCity(this);
                    popupWindowCity.setSelectCallBack(new PopupWindowCity.SelectCallBack() {
                        @Override
                        public void selectCallBack(String province, String city, String area, String location) {
                            yibaocun.setText(province + city + area);
                            locationId = location;
                        }
                    });
                }
                popupWindowCity.ShowPickerView();
//                showPicPopWindow(view);
                break;
            case R.id.submit:
                submit();
                break;
//            case R.id.paizhao:
//                Uri uri = Uri.fromFile(Utils.getLiZiPicFile());
//                takePhoto.onPickFromCaptureWithCrop(uri, getCropOptions());
//                break;
        }
    }

    String locationId;
    PopupWindowCity popupWindowCity;
    //分享弹出框
    LPopWindow lPopWindow;

    EditText prince;
    EditText oldPrice;

    private void showPicPopWindow(View view) {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pupopwinodw_edit_price, null);


        TextView confirm = contentView.findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lPopWindow.dismiss();
            }
        });


        prince = contentView.findViewById(R.id.prince);

        oldPrice = contentView.findViewById(R.id.oldPrice);


        lPopWindow = new LPopWindow.Builder(ReleaseXianZhiActivity.this)
                .setView(contentView)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, DimenUtil.dip2px(360))
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .enableBackgroundDark(false)
                .build();

    }

    private CropOptions getCropOptions() {

        CropOptions.Builder builder = new CropOptions.Builder();
//        builder.setWithOwnCrop(false);
        return builder.create();
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);

        if (result.getImage() != null && !TextUtils.isEmpty(result.getImage().getCompressPath())) {


            ArrayList<TImage> images = result.getImages();

            for (int i = 0; i < images.size(); i++) {
//                picData.add(images.get(i).getCompressPath());

                releaseWDPIcAdapter.itemDatas.add(releaseWDPIcAdapter.itemDatas.size() - 1, images.get(i).getCompressPath());

            }

//            picData.add(result.getImage().getCompressPath());
//            releaseWDPIcAdapter.itemDatas.add(releaseWDPIcAdapter.itemDatas.size() - 1, result.getImage().getCompressPath());
            releaseWDPIcAdapter.notifyDataSetChanged();
        }

//        ArrayList<String> itemDatas = new ArrayList();
//        itemDatas.addAll(picData);
//        itemDatas.add("res://com.superservice.lya/" + R.mipmap.ic_tianjia);

    }


    ReleaseWDPIcAdapter releaseWDPIcAdapter;

    private void initRecycleView() {
        ArrayList itemData = new ArrayList();
        itemData.add("res://com.superservice.lya/" + R.mipmap.ic_tianjia);

        releaseWDPIcAdapter = new ReleaseWDPIcAdapter(this, itemData);
        releaseWDPIcAdapter.setCallBack(new ReleaseWDPIcAdapter.CallBack() {
            @Override
            public void OnClick() {

                if (popupWindowCutPic == null) {
                    popupWindowCutPic = new PopupWindowCutPic(recycleView);
                    popupWindowCutPic.setOnClickListener(new PopupWindowCutPic.CutPicCallBack() {
                        @Override
                        public void choose() {
                            takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
//                            takePhoto.onPickFromGalleryWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                            takePhoto.onPickMultiple(6);
                        }

                        @Override
                        public void cancel() {

                        }

                        @Override
                        public void camera() {
                            takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
                            takePhoto.onPickFromCaptureWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                        }

                        @Override
                        public void see() {

                        }
                    });

                }

                popupWindowCutPic.isShow();
            }
        });

        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 3);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(releaseWDPIcAdapter);
    }

    PopupWindowCutPic popupWindowCutPic;
    TakePhoto takePhoto;

    PopupWindowGoodsType popupWindowGoodsType;

    private boolean isSubmit = false;

    private void submit() {

        if (isSubmit) {
            return;
        }

        if (TextUtils.isEmpty(etTitle.getText().toString())) {
            ToastUtil.show("标题不能为空");
            return;
        }
        if (TextUtils.isEmpty(etContent.getText().toString())) {
            ToastUtil.show("内容不能为空");
            return;
        }
        if (TextUtils.isEmpty(goodType)) {
            ToastUtil.show("请选择分类");
            return;
        }
        if (TextUtils.isEmpty(prince.getText().toString())) {
            ToastUtil.show("价格不能为空");
            return;
        }

        if (TextUtils.isEmpty(oldPrice.getText().toString())) {
            ToastUtil.show("原价不能为空");
            return;
        }


        if (TextUtils.isEmpty(locationId)) {
            ToastUtil.show("位置不能为空");
            return;
        }

        if (dialog == null) {
            dialog = CustomProgressDialog.creatRequestDialog(this);
        }
        dialog.show();

        Map maps = new HashMap();

        List<String> datas = new ArrayList();
        for (int i = 0; i < releaseWDPIcAdapter.itemDatas.size() - 1; i++) {
            datas.add("data:image/jpeg;base64," + Utils.imageToBase64(releaseWDPIcAdapter.itemDatas.get(i)));
        }

        maps.put("title", etTitle.getText().toString());
        maps.put("content", etContent.getText().toString());
        maps.put("price", prince.getText().toString());
        maps.put("oldPrice", oldPrice.getText().toString());
        maps.put("description", etContent.getText().toString());
        maps.put("type", "1");
        maps.put("locationId", locationId);

        String[] array = datas.toArray(new String[0]);


        HttpMehtod.getInstance().releaseGoods(maps, array, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean stringBaseDataBean) {
                isSubmit = false;
                dialog.dismiss();

                ToastUtil.show("发布成功");
                finish();
            }

            @Override
            public void onFail(int errorCode) {
                isSubmit = false;
                dialog.dismiss();
            }

        });

    }
}
